<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Controlling collection types</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.77.1">
<link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Boost.Bimap">
<link rel="up" href="../the_tutorial.html" title="The tutorial">
<link rel="prev" href="discovering_the_bimap_framework.html" title="Discovering the bimap framework">
<link rel="next" href="the_collection_of_relations_type.html" title="The collection of relations type">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="discovering_the_bimap_framework.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../the_tutorial.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="the_collection_of_relations_type.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section boost_bimap_the_tutorial_controlling_collection_types">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_bimap.the_tutorial.controlling_collection_types"></a><a class="link" href="controlling_collection_types.html" title="Controlling collection types">Controlling
      collection types</a>
</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="controlling_collection_types.html#boost_bimap.the_tutorial.controlling_collection_types.freedom_of_choice">Freedom
        of choice</a></span></dt>
<dt><span class="section"><a href="controlling_collection_types.html#boost_bimap.the_tutorial.controlling_collection_types.configuration_parameters">Configuration
        parameters</a></span></dt>
<dt><span class="section"><a href="controlling_collection_types.html#boost_bimap.the_tutorial.controlling_collection_types.examples">Examples</a></span></dt>
</dl></div>
<div class="section boost_bimap_the_tutorial_controlling_collection_types_freedom_of_choice">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_bimap.the_tutorial.controlling_collection_types.freedom_of_choice"></a><a class="link" href="controlling_collection_types.html#boost_bimap.the_tutorial.controlling_collection_types.freedom_of_choice" title="Freedom of choice">Freedom
        of choice</a>
</h4></div></div></div>
<p>
          As has already been said, in STL maps, you can only control the constraints
          from one of the collections, namely the one that you are viewing. In Boost.Bimap,
          you can control both and it is as easy as using the STL.
        </p>
<p>
          <span class="inlinemediaobject"><img src="../../images/bimap/extended.mapping.framework.png" alt="extended.mapping.framework"></span>
        </p>
<p>
          The idea is to use the same constraint names that are used in the standard.
          If you don't specify the collection type, Boost.Bimap assumes that the
          collection is a set. The instantiation of a bimap with custom collection
          types looks like this:
        </p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">bimap</span><span class="special">&lt;</span> <span class="bold"><strong>CollectionType</strong></span><span class="identifier">_of</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;,</span> <span class="bold"><strong>CollectionType</strong></span><span class="identifier">_of</span><span class="special">&lt;</span><span class="identifier">B</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">bm_type</span><span class="special">;</span>
</pre>
<p>
          The following is the list of all supported collection types.
        </p>
<div class="table">
<a name="boost_bimap.the_tutorial.controlling_collection_types.freedom_of_choice.t0"></a><p class="title"><b>Table&#160;1.2.&#160;Collection of Key Types</b></p>
<div class="table-contents"><table class="table" summary="Collection of Key Types">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    name
                  </p>
                </th>
<th>
                  <p>
                    Features
                  </p>
                </th>
<th>
                  <p>
                    map view type
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">set_of</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <span class="emphasis"><em>ordered, unique</em></span>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">map</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">multiset_of</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <span class="emphasis"><em>ordered </em></span>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">multimap</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">unordered_set_of</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <span class="emphasis"><em>hashed, unique </em></span>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">unordered_map</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">unordered_multiset_of</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <span class="emphasis"><em>hashed </em></span>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">unordered_multimap</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">list_of</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <span class="emphasis"><em>sequenced </em></span>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">list_map</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">vector_of</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <span class="emphasis"><em>random access </em></span>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">vector_map</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">unconstrained_set_of</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <span class="emphasis"><em>unconstrained </em></span>
                  </p>
                </td>
<td>
                  <p>
                    <span class="emphasis"><em>can not be viewed</em></span>
                  </p>
                </td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
          <code class="computeroutput"><span class="identifier">list_of</span></code> and <code class="computeroutput"><span class="identifier">vector_of</span></code> map views are not associated
          with any existing STL associative containers. They are two examples of
          unsorted associative containers. <code class="computeroutput"><span class="identifier">unconstrained_set_of</span></code>
          allow the user to ignore a view. This will be explained later.
        </p>
<p>
          <span class="inlinemediaobject"><img src="../../images/bimap/bimap.structures.png" alt="bimap.structures"></span>
        </p>
<p>
          The selection of the collection type affects the possible operations that
          you can perform with each side of the bimap and the time it takes to do
          each. If we have:
        </p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">bimap</span><span class="special">&lt;</span> <span class="bold"><strong>CollectionType</strong></span><span class="identifier">_of</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;,</span> <span class="bold"><strong>CollectionType</strong></span><span class="identifier">_of</span><span class="special">&lt;</span><span class="identifier">B</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">bm_type</span><span class="special">;</span>
<span class="identifier">bm_type</span> <span class="identifier">bm</span><span class="special">;</span>
</pre>
<p>
          The following now describes the resulting map views of the bidirectional
          map.
        </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
              <code class="computeroutput"><span class="identifier">bm</span><span class="special">.</span><span class="identifier">left</span></code> is signature-compatible with
              <span class="bold"><strong>LeftMapType</strong></span><code class="computeroutput"><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span><span class="identifier">B</span><span class="special">&gt;</span></code>
            </li>
<li class="listitem">
              <code class="computeroutput"><span class="identifier">bm</span><span class="special">.</span><span class="identifier">right</span></code> is signature-compatible with
              <span class="bold"><strong>RightMapType</strong></span><code class="computeroutput"><span class="special">&lt;</span><span class="identifier">B</span><span class="special">,</span><span class="identifier">A</span><span class="special">&gt;</span></code>
            </li>
</ul></div>
</div>
<div class="section boost_bimap_the_tutorial_controlling_collection_types_configuration_parameters">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_bimap.the_tutorial.controlling_collection_types.configuration_parameters"></a><a class="link" href="controlling_collection_types.html#boost_bimap.the_tutorial.controlling_collection_types.configuration_parameters" title="Configuration parameters">Configuration
        parameters</a>
</h4></div></div></div>
<p>
          Each collection type template has different parameters to control its behaviour.
          For example, in <code class="computeroutput"><span class="identifier">set_of</span></code>
          specification, you can pass a Functor type that compares two types. All
          of these parameters are exactly the same as those of the standard library
          container, except for the allocator type. You will learn later how to change
          the allocator for a bimap.
        </p>
<p>
          The following table lists the meanings of each collection type's parameters.
        </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    name
                  </p>
                </th>
<th>
                  <p>
                    Additional Parameters
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">set_of</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span><span class="identifier">KeyComp</span><span class="special">&gt;</span></code>
                  </p>
                  <p>
                    <code class="computeroutput"><span class="identifier">multiset_of</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span><span class="identifier">KeyComp</span><span class="special">&gt;</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <span class="bold"><strong>KeyComp </strong></span> is a Functor that compares
                    two types using a less-than operator. By default, this is <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">less</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>.
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">unordered_set_of</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span><span class="identifier">HashFunctor</span><span class="special">,</span><span class="identifier">EqualKey</span><span class="special">&gt;</span></code>
                  </p>
                  <p>
                    <code class="computeroutput"><span class="identifier">unordered_multiset_of</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span><span class="identifier">HashFunctor</span><span class="special">,</span><span class="identifier">EqualKey</span><span class="special">&gt;</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <span class="bold"><strong>HashFunctor </strong></span> converts a <code class="computeroutput"><span class="identifier">T</span></code> object into an <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span></code> value. By default it
                    is <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">hash</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>.
                  </p>
                  <p>
                    <span class="bold"><strong>EqualKey </strong></span> is a Functor that
                    tests two types for equality. By default, the equality operator
                    is <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>.
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">list_of</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
                  </p>
                </td>
<td>
                  <p>
                    No additional parameters.
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">vector_of</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
                  </p>
                </td>
<td>
                  <p>
                    No additional parameters.
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">unconstrained_set_of</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
                  </p>
                </td>
<td>
                  <p>
                    No additional parameters.
                  </p>
                </td>
</tr>
</tbody>
</table></div>
</div>
<div class="section boost_bimap_the_tutorial_controlling_collection_types_examples">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_bimap.the_tutorial.controlling_collection_types.examples"></a><a class="link" href="controlling_collection_types.html#boost_bimap.the_tutorial.controlling_collection_types.examples" title="Examples">Examples</a>
</h4></div></div></div>
<h6>
<a name="boost_bimap.the_tutorial.controlling_collection_types.examples.h0"></a>
          <span class="phrase"><a name="boost_bimap.the_tutorial.controlling_collection_types.examples.countries_populations"></a></span><a class="link" href="controlling_collection_types.html#boost_bimap.the_tutorial.controlling_collection_types.examples.countries_populations">Countries
          Populations</a>
        </h6>
<p>
          We want to store countries populations. The requirements are:
        </p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
              Get a list of countries in decreasing order of their populations.
            </li>
<li class="listitem">
              Given a country, get their population.
            </li>
</ol></div>
<p>
          Lets create the appropriate bimap.
        </p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">bimap</span><span class="special">&lt;</span>

    <span class="identifier">unordered_set_of</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;,</span>
    <span class="identifier">multiset_of</span><span class="special">&lt;</span> <span class="keyword">long</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">greater</span><span class="special">&lt;</span><span class="keyword">long</span><span class="special">&gt;</span> <span class="special">&gt;</span>

<span class="special">&gt;</span> <span class="identifier">populations_bimap</span><span class="special">;</span>
</pre>
<p>
          First of all countries names are unique identifiers, while two countries
          may have the same population. This is why we choose <span class="bold"><strong>multi</strong></span><code class="computeroutput"><span class="identifier">set_of</span></code> for populations.
        </p>
<p>
          Using a <code class="computeroutput"><span class="identifier">multiset_of</span></code> for
          population allow us to iterate over the data. Since listing countries ordered
          by their names is not a requisite, we can use an <code class="computeroutput"><span class="identifier">unordered_set_of</span></code>
          that allows constant order look up.
        </p>
<p>
          And now lets use it in a complete example
        </p>
<p>
          <a href="../../../../example/population_bimap.cpp" target="_top">Go to source code</a>
        </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">bimap</span><span class="special">&lt;</span>

    <span class="identifier">unordered_set_of</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;,</span>
    <span class="identifier">multiset_of</span><span class="special">&lt;</span> <span class="keyword">long</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">greater</span><span class="special">&lt;</span><span class="keyword">long</span><span class="special">&gt;</span> <span class="special">&gt;</span>

<span class="special">&gt;</span> <span class="identifier">population_bimap</span><span class="special">;</span>

<span class="keyword">typedef</span> <span class="identifier">population_bimap</span><span class="special">::</span><span class="identifier">value_type</span> <span class="identifier">population</span><span class="special">;</span>

<span class="identifier">population_bimap</span> <span class="identifier">pop</span><span class="special">;</span>
<span class="identifier">pop</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">population</span><span class="special">(</span><span class="string">"China"</span><span class="special">,</span>          <span class="number">1321000000</span><span class="special">)</span> <span class="special">);</span>
<span class="identifier">pop</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">population</span><span class="special">(</span><span class="string">"India"</span><span class="special">,</span>          <span class="number">1129000000</span><span class="special">)</span> <span class="special">);</span>
<span class="identifier">pop</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">population</span><span class="special">(</span><span class="string">"United States"</span><span class="special">,</span>   <span class="number">301950000</span><span class="special">)</span> <span class="special">);</span>
<span class="identifier">pop</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">population</span><span class="special">(</span><span class="string">"Indonesia"</span><span class="special">,</span>       <span class="number">234950000</span><span class="special">)</span> <span class="special">);</span>
<span class="identifier">pop</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">population</span><span class="special">(</span><span class="string">"Brazil"</span><span class="special">,</span>          <span class="number">186500000</span><span class="special">)</span> <span class="special">);</span>
<span class="identifier">pop</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">population</span><span class="special">(</span><span class="string">"Pakistan"</span><span class="special">,</span>        <span class="number">163630000</span><span class="special">)</span> <span class="special">);</span>

<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Countries by their population:"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>

<span class="comment">// First requirement</span>
<a class="co" name="boost_bimap.the_tutorial.controlling_collection_types.examples.c0" href="controlling_collection_types.html#boost_bimap.the_tutorial.controlling_collection_types.examples.c1"><img src="../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a><span class="keyword">for</span><span class="special">(</span> <span class="identifier">population_bimap</span><span class="special">::</span><span class="identifier">right_const_iterator</span>
        <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">pop</span><span class="special">.</span><span class="identifier">right</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">iend</span> <span class="special">=</span> <span class="identifier">pop</span><span class="special">.</span><span class="identifier">right</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span>
        <span class="identifier">i</span> <span class="special">!=</span> <span class="identifier">iend</span> <span class="special">;</span> <span class="special">++</span><span class="identifier">i</span> <span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">-&gt;</span><span class="identifier">second</span> <span class="special">&lt;&lt;</span> <span class="string">" with "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">-&gt;</span><span class="identifier">first</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>

<span class="comment">// Second requirement</span>
<a class="co" name="boost_bimap.the_tutorial.controlling_collection_types.examples.c2" href="controlling_collection_types.html#boost_bimap.the_tutorial.controlling_collection_types.examples.c3"><img src="../../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Population of China: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">pop</span><span class="special">.</span><span class="identifier">left</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="string">"China"</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
</pre>
<p>
        </p>
<div class="calloutlist"><table border="0" summary="Callout list">
<tr>
<td width="5%" valign="top" align="left"><p><a name="boost_bimap.the_tutorial.controlling_collection_types.examples.c1"></a><a href="#boost_bimap.the_tutorial.controlling_collection_types.examples.c0"><img src="../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td>
<td valign="top" align="left"><p>
              The right map view works like a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">multimap</span><span class="special">&lt;</span> <span class="keyword">long</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">greater</span><span class="special">&lt;</span><span class="keyword">long</span><span class="special">&gt;</span> <span class="special">&gt;</span></code>,
              We can iterate over it to print the results in the required order.
            </p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><a name="boost_bimap.the_tutorial.controlling_collection_types.examples.c3"></a><a href="#boost_bimap.the_tutorial.controlling_collection_types.examples.c2"><img src="../../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td>
<td valign="top" align="left"><p>
              The left map view works like a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unordered_map</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">long</span> <span class="special">&gt;</span></code>, given the name of the country
              we can use it to search for the population in constant time
            </p></td>
</tr>
</table></div>
<h6>
<a name="boost_bimap.the_tutorial.controlling_collection_types.examples.h1"></a>
          <span class="phrase"><a name="boost_bimap.the_tutorial.controlling_collection_types.examples.repetitions_counter"></a></span><a class="link" href="controlling_collection_types.html#boost_bimap.the_tutorial.controlling_collection_types.examples.repetitions_counter">Repetitions
          counter</a>
        </h6>
<p>
          We want to count the repetitions for each word in a text and print them
          in order of appearance.
        </p>
<p>
          <a href="../../../../example/repetitions_counter.cpp" target="_top">Go to source code</a>
        </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">bimap</span>
<span class="special">&lt;</span>
    <span class="identifier">unordered_set_of</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&gt;,</span>
    <span class="identifier">list_of</span><span class="special">&lt;</span> <span class="identifier">counter</span> <span class="special">&gt;</span> <a class="co" name="boost_bimap.the_tutorial.controlling_collection_types.examples.c4" href="controlling_collection_types.html#boost_bimap.the_tutorial.controlling_collection_types.examples.c5"><img src="../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a>

<span class="special">&gt;</span> <span class="identifier">word_counter</span><span class="special">;</span>

<span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">tokenizer</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">char_separator</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">text_tokenizer</span><span class="special">;</span>

<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">text</span><span class="special">=</span>
    <span class="string">"Relations between data in the STL are represented with maps."</span>
    <span class="string">"A map is a directed relation, by using it you are representing "</span>
    <span class="string">"a mapping. In this directed relation, the first type is related to "</span>
    <span class="string">"the second type but it is not true that the inverse relationship "</span>
    <span class="string">"holds. This is useful in a lot of situations, but there are some "</span>
    <span class="string">"relationships that are bidirectional by nature."</span><span class="special">;</span>

<span class="comment">// feed the text into the container</span>
<span class="identifier">word_counter</span>   <span class="identifier">wc</span><span class="special">;</span>
<span class="identifier">text_tokenizer</span> <span class="identifier">tok</span><span class="special">(</span><span class="identifier">text</span><span class="special">,</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">char_separator</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;(</span><span class="string">" \t\n.,;:!?'\"-"</span><span class="special">));</span>

<span class="keyword">for</span><span class="special">(</span> <span class="identifier">text_tokenizer</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">tok</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">it_end</span> <span class="special">=</span> <span class="identifier">tok</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span>
     <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">it_end</span> <span class="special">;</span> <span class="special">++</span><span class="identifier">it</span> <span class="special">)</span>
<span class="special">{</span>
    <a class="co" name="boost_bimap.the_tutorial.controlling_collection_types.examples.c6" href="controlling_collection_types.html#boost_bimap.the_tutorial.controlling_collection_types.examples.c7"><img src="../../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a><span class="special">++</span> <span class="identifier">wc</span><span class="special">.</span><span class="identifier">left</span><span class="special">[*</span><span class="identifier">it</span><span class="special">];</span>
<span class="special">}</span>

<span class="comment">// list words with counters by order of appearance</span>
<a class="co" name="boost_bimap.the_tutorial.controlling_collection_types.examples.c8" href="controlling_collection_types.html#boost_bimap.the_tutorial.controlling_collection_types.examples.c9"><img src="../../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a><span class="keyword">for</span><span class="special">(</span> <span class="identifier">word_counter</span><span class="special">::</span><span class="identifier">right_const_iterator</span>
        <span class="identifier">wit</span> <span class="special">=</span> <span class="identifier">wc</span><span class="special">.</span><span class="identifier">right</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">wit_end</span> <span class="special">=</span> <span class="identifier">wc</span><span class="special">.</span><span class="identifier">right</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span>

     <span class="identifier">wit</span> <span class="special">!=</span> <span class="identifier">wit_end</span><span class="special">;</span> <span class="special">++</span><span class="identifier">wit</span> <span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">wit</span><span class="special">-&gt;</span><span class="identifier">second</span> <span class="special">&lt;&lt;</span> <span class="string">": "</span> <span class="special">&lt;&lt;</span> <span class="identifier">wit</span><span class="special">-&gt;</span><span class="identifier">first</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
        </p>
<div class="calloutlist"><table border="0" summary="Callout list">
<tr>
<td width="5%" valign="top" align="left"><p><a name="boost_bimap.the_tutorial.controlling_collection_types.examples.c5"></a><a href="#boost_bimap.the_tutorial.controlling_collection_types.examples.c4"><img src="../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td>
<td valign="top" align="left"><p>
              <code class="computeroutput"><span class="identifier">counter</span></code> is an integer
              that is initialized in zero in the constructor
            </p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><a name="boost_bimap.the_tutorial.controlling_collection_types.examples.c7"></a><a href="#boost_bimap.the_tutorial.controlling_collection_types.examples.c6"><img src="../../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td>
<td valign="top" align="left"><p>
              Because the right collection type is <code class="computeroutput"><span class="identifier">list_of</span></code>,
              the right data is not used a key and can be modified in the same way
              as with standard maps.
            </p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><a name="boost_bimap.the_tutorial.controlling_collection_types.examples.c9"></a><a href="#boost_bimap.the_tutorial.controlling_collection_types.examples.c8"><img src="../../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> </p></td>
<td valign="top" align="left"><p>
              When we insert the elements using the left map view, the element is
              inserted at the end of the list.
            </p></td>
</tr>
</table></div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2006-2012 Matias Capeletto<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="discovering_the_bimap_framework.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../the_tutorial.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="the_collection_of_relations_type.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
